﻿using EfDomain.Entities;
using EfInfra;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;

namespace EfLearn.Uniter
{
    internal class Queryer
    {
        public static void Simple()
        {
            //using (var db = Factory.CreateDbContext())
            //{
            //    var ret=db.Category
            //        //.Include(x=>x.Products)
            //        .FirstOrDefault();
            //    Console.WriteLine(ret?.Products?.Count() ?? 0);

            //    var retRich = db.Entry(ret)
            //        .Collection(b => b.Products)
            //        .Query()
            //        .ToList();

            //    Console.WriteLine(retRich?.Count());
            //}

            var db = DI.InsProvider.GetService<MyContext>();
            {
                var ret = db!.Blog.ToList();
                Console.WriteLine(ret?.FirstOrDefault()?.Id);
            }
        }

        public static void GetContextInfo()
        {

            var db = DI.InsProvider.GetService<MyContext>();

            Console.WriteLine(db.ContextId.InstanceId.ToString());
        }

        public static void QuerySql()
        {
            var db = DI.InsProvider.GetService<MyContext>();
            string sql= @"SELECT T.OWNER Owner,
T.TABLE_NAME ObjName,
C.COMMENTS as ""Comment"",
'TABLE' ObjType
FROM ALL_TABLES T
    LEFT JOIN ALL_TAB_COMMENTS C
    ON T.TABLE_NAME = C.TABLE_NAME
    AND T.OWNER = C.OWNER
    AND C.TABLE_TYPE = 'TABLE'
WHERE T.OWNER='JYHF1232'";

            var ret=db.Database.SqlQueryRaw<MetaObj>(sql).ToList();
            Console.WriteLine(ret.Count());
        }
    }
}
